package com.easy.customer.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easy.common.bean.Customer;
import com.easy.common.bean.CustomerContact;
import com.easy.common.bean.CustomerInteraction;
import com.easy.common.service.ICustomerContactService;
import com.easy.common.service.ICustomerInteractionService;
import com.easy.common.service.ICustomerService;
import com.easy.common.util.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("customer")
public class CustomerController {

    @Autowired
    private ICustomerService customerService;

    @Autowired
    private ICustomerContactService customerContactService;

    @Autowired
    private ICustomerInteractionService customerInteractionService;

    // Customer 相关接口

    @PostMapping("/customer")
    public ResultData insertCustomer(@RequestBody Customer customer) {
        Customer data = customerService.insert(customer);
        return ResultData.success(data);
    }

    @PutMapping("/customer")
    public ResultData updateCustomer(@RequestBody Customer customer) {
        Customer data = customerService.update(customer);
        return ResultData.success(data);
    }

    @DeleteMapping("/customer/{id}")
    public ResultData deleteCustomer(@PathVariable Integer id) {
        int result = customerService.delete(id);
        if (result >= 1) {
            return ResultData.success("200", "删除成功", null);
        } else {
            return ResultData.fail();
        }
    }

    @GetMapping("/customer/{id}")
    public ResultData getCustomerById(@PathVariable Integer id) {
        Customer data = customerService.getById(id);
        return ResultData.success(data);
    }

    @GetMapping("/customer/page")
    public ResultData getCustomerListByPage(Page page, Customer customer) {
        IPage<Customer> data = customerService.getListByPage(page, customer);
        return ResultData.success(data);
    }

    @GetMapping("/customer/list")
    public ResultData getCustomerList(Customer customer) {
        java.util.List<Customer> data = customerService.getList(customer);
        return ResultData.success(data);
    }

    // CustomerContact 相关接口

    @PostMapping("/contact")
    public ResultData insertContact(@RequestBody CustomerContact customerContact) {
        CustomerContact data = customerContactService.insert(customerContact);
        return ResultData.success(data);
    }

    @PutMapping("/contact")
    public ResultData updateContact(@RequestBody CustomerContact customerContact) {
        CustomerContact data = customerContactService.update(customerContact);
        return ResultData.success(data);
    }

    @DeleteMapping("/contact/{id}")
    public ResultData deleteContact(@PathVariable Integer id) {
        int result = customerContactService.delete(id);
        if (result >= 1) {
            return ResultData.success("200", "删除成功", null);
        } else {
            return ResultData.fail();
        }
    }

    @PostMapping("/contact/setPrimary/{id}")
    public ResultData setPrimaryContact(@PathVariable Integer id) {
        int result = customerContactService.setPrimary(id);
        if (result >= 1) {
            return ResultData.success("200", "设置主要联系方式成功", null);
        } else {
            return ResultData.fail();
        }
    }

    // CustomerInteraction 相关接口

    @PostMapping("/interaction")
    public ResultData insertInteraction(@RequestBody CustomerInteraction customerInteraction) {
        CustomerInteraction data = customerInteractionService.insert(customerInteraction);
        return ResultData.success(data);
    }

    @PutMapping("/interaction")
    public ResultData updateInteraction(@RequestBody CustomerInteraction customerInteraction) {
        CustomerInteraction data = customerInteractionService.update(customerInteraction);
        return ResultData.success(data);
    }

    @DeleteMapping("/interaction/{id}")
    public ResultData deleteInteraction(@PathVariable Integer id) {
        int result = customerInteractionService.delete(id);
        if (result >= 1) {
            return ResultData.success("200", "删除成功", null);
        } else {
            return ResultData.fail();
        }
    }

    @GetMapping("/interaction/page")
    public ResultData getInteractionListByPage(Page page) {
        IPage<CustomerInteraction> data = customerInteractionService.getListByPage(page);
        return ResultData.success(data);
    }
}